package com.gmrz.uaf.protocol.v1.schema;

import com.gmrz.uaf.common.TLVType2;
import com.gmrz.uaf.protocol.v1.json.UAFSchemaBuilder;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ServerData extends TLVType2 {
    public transient byte[] challenge;
    public transient long timestamp;
    public transient String userName;
    public transient byte operationId;
    public transient String policyName;
    public transient String bindData;
    public transient Set<String> transactionHash = new HashSet<String>();
    public transient Map<String, String> validateDataHash = new HashMap<String, String>();
    private String encodedServerData;

    public byte[] getChallenge() {
        return this.challenge;
    }

    public void setChallenge(byte[] challenge) {
        this.challenge = challenge;
    }

    public ServerData withChallenge(byte[] challenge) {
        this.challenge = challenge;
        return this;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public ServerData withTimestamp(long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ServerData withUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public byte getOperationId() {
        return this.operationId;
    }

    public void setOperationId(byte operationId) {
        this.operationId = operationId;
    }

    public String getPolicyName() {
        return this.policyName;
    }

    public void setPolicyName(String policyName) {
        this.policyName = policyName;
    }

    public ServerData withPolicyName(String policyName) {
        this.policyName = policyName;
        return this;
    }

    public String getBindData() {
        return this.bindData;
    }

    public void setBindData(String bindData) {
        this.bindData = bindData;
    }

    public ServerData withBindData(String bindData) {
        this.bindData = bindData;
        return this;
    }

    public ServerData addTransactionHash(String b64Hash) {
        this.transactionHash.add(b64Hash);
        return this;
    }

    public Set<String> getTransactionHashSet() {
        return this.transactionHash;
    }

    public String getEncodedServerData() {
        return this.encodedServerData;
    }

    public void setEncodedServerData(String encodedServerData) {
        this.encodedServerData = encodedServerData;
    }

    public ServerData withEncodedServerData(String encodedServerData) {
        this.encodedServerData = encodedServerData;
        return this;
    }

    public void putValidateDataHash(String key, String hash) {
        validateDataHash.put(key, hash);
    }

    public String getValidateDataHashToString() {
        Gson gson = UAFSchemaBuilder.getGson();
        String validateDataHashJson = gson.toJson(validateDataHash);
        return validateDataHashJson;
    }

    public void setStringToValidateDataHash(String validateDataHashJson){
        Gson gson = UAFSchemaBuilder.getGson();
        HashMap validateDataHashMap = gson.fromJson(validateDataHashJson,HashMap.class);
        validateDataHash.putAll(validateDataHashMap);
    }

    public String getValidateDataHash(String key){
        return validateDataHash.get(key);
    }

    public String toString() {
        return new StringBuilder().append("ServerData [challenge.length=").append(this.challenge == null ? "0" : String.valueOf(this.challenge.length)).append(", timestamp=").append(this.timestamp).append(", userName=").append(this.userName).append(", operationId=").append(this.operationId).append(", policyName=").append(this.policyName).append(", bindData.length=").append(this.bindData == null ? "0" : String.valueOf(this.bindData.length())).append(this.transactionHash != null ? new StringBuilder().append(", transaction.hash=").append(this.transactionHash).toString() : "").append("]").toString();
    }

}
